<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/19
 * Time: 18:21
 */
class 用迭代实现数组的快速排序
{

}

function quicksortX(&$seq)
{
    $stack = array($seq);
    $sort = array();
    while ($stack) {
        $arr = array_pop($stack);
        if(count($arr) <= 1){
            if (count($arr) == 1) {
                $sort[] = &$arr[0];
            }
            continue;
        }
        $k = $arr[0];
        $x = array();
        $y = array();
        $_size = count($arr);
        for ($i = 1; $i < $_size; $i++) {
            if ($arr[$i] <= $k) {
                $x[] = &$arr[$i];
            } else {
                $y[] = &$arr[$i];
            }
        }
        !empty($y) && array_push($stack, $y);
        array_push($stack, array($arr[0]));
        !empty($x) && array_push($stack, $x);
    }
    return $sort;
}
